package cn.xn.hrms.api.controller;

import cn.xn.hrms.domain.enums.ResultEnum;
import cn.xn.hrms.domain.utils.QiniuUtils;
import cn.xn.hrms.domain.utils.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author: ysz
 * @date: 2023/08/30
 **/
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private QiniuUtils qiniuUtils;


    /**
     * 文件上传
     *
     * @param multipartFile
     * @return
     */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ResultVo doUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(multipartFile.getOriginalFilename(), ".");
        boolean uploadResult = qiniuUtils.upload(multipartFile, fileName);
        if (uploadResult){
            return ResultVo.uploadSuccess(qiniuUtils.getUrl() + fileName);
        }

        return ResultVo.fail(ResultEnum.UPLOAD_ERROR);

    }


}
